const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const curTime = () => {
  return formatTime(new Date())
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const showMessage = (e, dur = 1500) => {
  uni.showToast({
    title: e,
    icon: 'none',
    duration: dur
  })
}
const showSuccess = (e, dur = 1500) => {
  uni.showToast({
    title: e,
    icon: 'success',
    duration: dur
  })
}

const showLoading = (e) => {
  uni.showLoading({ 
    title: e,
  })
}

const hideLoading = () => {
  uni.hideLoading()
}

//base64转码
const Base64 = function () {
  // private property  
  let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

  // public method for encoding  
  this.encode = function (input) {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;
    input = _utf8_encode(input);
    while (i < input.length) {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);
      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;
      if (isNaN(chr2)) {
        enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
        enc4 = 64;
      }
      output = output +
        _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
        _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
    }
    return output;
  }

  // private method for UTF-8 encoding  
  let _utf8_encode = function (string) {
    string = string.replace(/\r\n/g, "\n");
    var utftext = "";
    for (var n = 0; n < string.length; n++) {
      var c = string.charCodeAt(n);
      if (c < 128) {
        utftext += String.fromCharCode(c);
      } else if ((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      } else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }

    }
    return utftext;
  }
}
//日期格式转换
//精确到秒
const dateChangeToSecond = date => {
  if (date) {
    let newData = new Date(date)
    let year = newData.getFullYear()
    let month = newData.getMonth() + 1
    let day = newData.getDate()
    let hour = newData.getHours()
    let minute = newData.getMinutes()
    let second = newData.getSeconds()

    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    if (hour < 10) {
      hour = '0' + hour
    }
    if (minute < 10) {
      minute = '0' + minute
    }
    if (second < 10) {
      second = '0' + second
    }
    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
  } else {
    return ''
  }
}
//精确到天
const dateChangeToDay = date => {
  if (date) {
    let newData = new Date(date.replace(/-/g, '/'))
    let year = newData.getFullYear()
    let month = newData.getMonth() + 1
    let day = newData.getDate()
    return year + '-' + month + '-' + day;
  } else {
    return ''
  }
}
// 精确到天，给手机展示用
const dateChangeToDays = date => {
  if (date) {
    let newData = new Date(date)
    let year = newData.getFullYear()
    let month = newData.getMonth() + 1
    let day = newData.getDate()
    return year + '-' + month + '-' + day;
  } else {
    return ''
  }
}
// 精确到月
const dateChangeToMonth = date => {
  if (date) {
    let newData = new Date(date.replace(/-/g, '/'))
    let year = newData.getFullYear()
    let month = newData.getMonth() + 1
    return year + '-' + month;
  } else {
    return ''
  }
}
// 获取当前日期
const getCurDay = () => {
  let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  return year + '-' + month + '-' + day
}
// 获取当前月份
const getCurMonth = () => {
  let date = new Date()
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  if (month < 10) {
    month = '0' + month
  }
  return year + '-' + month
}
// 计算时间距离当前时间时间差
const computeTime = date => {
  if (date) {
    let nowTime = new Date()
    let time = new Date(date.replace(/-/g, '/'))
    let differTime = (nowTime - time) / 1000
    if (differTime < 60) {
      return '刚刚' //一分钟内
    } else if (differTime < (60 * 60)) {
      //多少分钟前
      return Math.floor((differTime / 60)) + '分钟前'
    } else if (differTime < (24 * 60 * 60)) {
      //多少小时前
      return Math.floor((differTime / (60 * 60))) + '小时前'
    } else if (differTime < (30 * 24 * 60 * 60)) {
      //多少天前
      return Math.floor((differTime / (24 * 60 * 60))) + '天前'
    } else if (differTime < (12 * 30 * 24 * 60 * 60)) {
      //多少月前
      return Math.floor((differTime / (30 * 24 * 60 * 60))) + '月前'
    } else {
      //多少年前
      return Math.floor((differTime / (12 * 30 * 24 * 60 * 60))) + '年前'
    }
  } else {
    return ''
  }
}

// 计算支付倒计时
const calcPayCountdown = date => {
  let hour = 0
  let minute = 0

  let nowTime = new Date()
  let finallyPayTime = new Date(date)
  // 相差分钟数
  let differ = finallyPayTime - nowTime
  let temp = differ % 60000
  let differMinute = (differ - temp) / 60000

  // 计算返回值
  if (differMinute < 60) {
    // 如果分钟数不足一小时
    minute = differMinute
    hour = 0
  } else {
    // 如果分钟数超过一小时
    minute = differMinute % 60
    hour = (differMinute - minute) / 60
  }

  return { hour: hour, minute: minute }
}

const finallyPayTime = date => {
  let createTime = new Date(date)
  return createTime.setHours(createTime.getHours() + 24)
}

const toDate = date=> {
  var date = new Date(date);
  var YY = date.getFullYear() + '-';
  var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
  var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  // return YY + MM + DD +" "+hh + mm + ss;
  return [YY + MM + DD, hh + mm];
}

const bytesToSize = bytes => {
  if (bytes === 0) return '0 B';
  var k = 1000, // or 1024
    sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
    i = Math.floor(Math.log(bytes) / Math.log(k));
  return [(bytes / Math.pow(k, i)).toFixed(2), sizes[i]];
}

const formatSeconds = (value) =>{ 
 var theTime = parseInt(value);// 需要转换的时间秒 
 var theTime1 = 0;// 分 
 var theTime2 = 0;// 小时 
 var theTime3 = 0;// 天
 if(theTime > 60) { 
  theTime1 = parseInt(theTime/60); 
  theTime = parseInt(theTime%60); 
  if(theTime1 > 60) { 
   theTime2 = parseInt(theTime1/60); 
   theTime1 = parseInt(theTime1%60); 
   if(theTime2 > 24){
    //大于24小时
    theTime3 = parseInt(theTime2/24);
    theTime2 = parseInt(theTime2%24);
   }
  } 
 } 
 var result = '';
 if(theTime2 > 0) { 
  result = ""+parseInt(theTime2)+"小时"+result; 
 } 
 if(theTime3 > 0) { 
  result = ""+parseInt(theTime3)+"天"+result; 
 }
 return result; 
}


const formatDate1 =  (date, fmt) => {
  if (typeof date == 'string') {
    return date;
  }

  if (!fmt) fmt = "yyyy-MM-dd hh:mm:ss";

  if (!date || date == null) return null;
  var o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  }
  return fmt
}
const util = {
  formatTime,
  showMessage,
  showSuccess,
  Base64,
  dateChangeToSecond,
  dateChangeToDay,
  dateChangeToDays,
  dateChangeToMonth,
  getCurDay,
  getCurMonth,
  computeTime,
  calcPayCountdown,
  finallyPayTime,
  showLoading,
  hideLoading,
  curTime,
  toDate,
  bytesToSize,
	formatSeconds,
	formatDate1
}


export default util